package com.yidu.web.utils;

import com.yidu.web.model.Crosstalklist;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * 爬虫类
 */
public class HTMLTools {
    public static void main(String[] args) {

        try {
            //jokeTitle(URL);
            String str = stringToMD5("12345678901123456789456645645623165dsadasdasfsdfsdfsdfdsfsdfdsfdsfsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsdsd");
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String URL = "http://www.qiushibaike.net/index(2005).html";

    public static String RURL="http://www.qiushibaike.net/"; //根网址

    /**
     * 爬笑话的标题
     */
    public static List<Crosstalklist> paCrossTalkTitle(String url) throws IOException {
        List<Crosstalklist> list = new ArrayList();

        Document doc = Jsoup.connect(URL).get();
        Elements elements = doc.select(".Volume a[href]");

        for(Element element : elements){
            System.out.println(element.text()+"  " + element.attr("href"));
            String subUrl = element.attr("href");

            if(subUrl.indexOf(".html")>-1) {
                Crosstalklist crosstalklist = null;//找笑话内容
                try {
                    crosstalklist = crosstalkContent(subUrl);
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }
                crosstalklist.setCrossTalkTitle(element.text());
                crosstalklist.setFlag(stringToMD5(crosstalklist.getCrossTalkTitle()+crosstalklist.getCrossTalkContent()));//编码
                crosstalklist.setCrossTalkA(subUrl);

                list.add(crosstalklist);
                try {
                    Thread.sleep(520);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        return list;
    }


    public static Crosstalklist crosstalkContent(String url) throws IOException {

        Document document = Jsoup.connect(RURL+url).get();//建立连接并且得到HTML文档\
        Crosstalklist crosstalklist = new Crosstalklist();
        //核心代码

        StringBuffer content = new StringBuffer("");
        Elements elements = document.select(".content").select("p");

        for(Element element : elements){
            content.append(element.text());//追加
        }

        crosstalklist.setCrossTalkContent(content.toString());



        return crosstalklist;
    }

    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("没有这个md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
}
